package com.ruoyi.edu.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.ruoyi.common.core.constant.CourseStatusConstants;
import com.ruoyi.edu.api.domain.vo.EduCourseVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.edu.api.domain.EduCourse;
import com.ruoyi.edu.service.IEduCourseService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 课程Controller
 *
 * @author isahino
 * @date 2023-09-14
 */
@RestController
@RequestMapping("/course")
public class EduCourseController extends BaseController {
    @Autowired
    private IEduCourseService eduCourseService;

    /**
     * 查询课程列表
     */
    @RequiresPermissions("edu:course:list")
    @GetMapping("/list")
    public TableDataInfo list(EduCourse eduCourse, Integer pageNum, Integer pageSize) {
        List<EduCourseVO> list = eduCourseService.selectEduCourseList(eduCourse);
        return getDataTableVO(list, pageNum, pageSize);
    }

    /**
     * 查询课程列表 - 下拉框用
     */
    @GetMapping("/all")
    public AjaxResult all() {
        return AjaxResult.success(eduCourseService.list());
    }

    /**
     * 导出课程列表
     */
    @RequiresPermissions("edu:course:export")
    @Log(title = "课程", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, EduCourse eduCourse) {
        List<EduCourseVO> list = eduCourseService.selectEduCourseList(eduCourse);
        ExcelUtil<EduCourseVO> util = new ExcelUtil<>(EduCourseVO.class);
        util.exportExcel(response, list, "课程数据");
    }

    /**
     * 获取课程详细信息
     */
    @RequiresPermissions("edu:course:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        return success(eduCourseService.selectEduCourseById(id));
    }

    /**
     * 新增课程
     */
    @RequiresPermissions("edu:course:add")
    @Log(title = "课程", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody EduCourse eduCourse) {
        return toAjax(eduCourseService.insertEduCourse(eduCourse));
    }

    /**
     * 修改课程
     */
    @RequiresPermissions("edu:course:edit")
    @Log(title = "课程", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody EduCourse eduCourse) {
        return toAjax(eduCourseService.updateEduCourse(eduCourse));
    }

    /**
     * 删除课程
     */
    @RequiresPermissions("edu:course:remove")
    @Log(title = "课程", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) {
        return toAjax(eduCourseService.deleteEduCourseByIds(ids));
    }

    /**
     * 发布或上架课程
     */
    @RequiresPermissions("edu:course:edit")
    @Log(title = "课程", businessType = BusinessType.UPDATE)
    @PutMapping("/release")
    public AjaxResult release(@RequestBody EduCourse eduCourse) {
        LambdaUpdateWrapper<EduCourse> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(EduCourse::getId, eduCourse.getId());
        if (CourseStatusConstants.UN_RELEASE.equals(eduCourse.getStatus())) {
            wrapper.set(EduCourse::getStatus, CourseStatusConstants.RELEASED);
        } else {
            wrapper.set(EduCourse::getStatus, CourseStatusConstants.UN_RELEASE);
        }
        return toAjax(eduCourseService.update(wrapper));
    }
}
